package Student

import (
	"chinasoccer/models"
	"chinasoccer/pkg/app"
	"chinasoccer/pkg/e"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func UpsertStudent(c *gin.Context) {
	err, info := models.UpsertStudent(c)
	if err != nil {
		app.Fail(c, constant.ERROR, err, err.Error())
		return
	}
	app.Success(c, info, "success")
}

func AddStudent(c *gin.Context) {
	err, info := models.AddStudent(c)
	if err != nil {
		app.Fail(c, constant.ERROR, err, err.Error())
		return
	}
	app.Success(c, info, "success")
}

func GetAllStudents(c *gin.Context) {
	page := -1
	limit := 100
    searchText := ""
	if arg := c.Query("page"); arg != "" {
		page = com.StrTo(arg).MustInt()
	}
	if arg := c.Query("limit"); arg != "" {
		limit = com.StrTo(arg).MustInt()
	}
	if arg := c.Query("searchText"); arg != "" {
		searchText = arg
	}
	getAllStudentParamInput := map[string]interface{}{
		"page":       page,
		"limit":      limit,
		"Sname":      c.Query("Sname"),
		"searchText": searchText,
	}
	err, info, total := models.GetAllStudents(getAllStudentParamInput)
	if err != nil {
		app.Fail(c, constant.ERROR, err, err.Error())
		return
	}
	app.Success(c, map[string]interface{}{"value": info, "total": total}, "Success")
}

func UpdateStudent(c *gin.Context) {
	err, info := models.UpdateStudent(c)
	if err != nil {
		app.Fail(c, constant.ERROR, err, err.Error())
		return
	}
	app.Success(c, info, "success")
}